home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Plus 2004 #11
/
Amiga Plus CD - 2004 - No. 11.iso
/
AmiSoft
/
Dev
/
misc
/
LOCCounter.lha
/
LOCCounter
/
src
/
PatternMatch.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2004-08-17
|
3KB
|
116 lines
/****************************************************************************
*
* $RCSfile: PatternMatch.cpp $
* $Revision: 2.6 $
* $Date: 2004/08/17 22:38:58 $
* $Author: ssolie $
*
*****************************************************************************
*
* Copyright (c) 2004 Steven Solie. All Rights Reserved.
*
*****************************************************************************
*
* PatternMatch component
*
* The PatternMatch class is used to match AmigaOS patterns.
*/
#include "PatternMatch.h"
#include <dos/anchorpath.h>
#include <dos/dos.h>
#include <dos/dosasl.h>
#include <exec/memory.h>
#include <proto/dos.h>
#include <new>
PatternMatch::PatternMatch() :
m_anchor(0),
m_lock(0),
m_searching(false)
{
m_anchor = reinterpret_cast<AnchorPath*>(IDOS->AllocDosObjectTags(
DOS_ANCHORPATH,
ADO_Strlen, 0,
ADO_Flags, 0,
ADO_Mask, SIGBREAKF_CTRL_C,
TAG_END));
if ( m_anchor == 0 ) {
throw std::bad_alloc();
}
}
PatternMatch::~PatternMatch()
{
if ( m_searching ) {
IDOS->CurrentDir(m_lock); // restore lock
IDOS->MatchEnd(m_anchor);
}
IDOS->FreeDosObject(DOS_ANCHORPATH, m_anchor);
}
const char* PatternMatch::firstFile(const char* pattern)
{
if ( m_searching ) {
IDOS->SetIoErr(ERROR_OBJECT_NOT_FOUND);
throw dos_error();
}
int32 match = IDOS->MatchFirst(const_cast<STRPTR>(pattern), m_anchor);
while ( match == 0 && m_anchor->ap_Info.fib_DirEntryType > 0 ) {
m_anchor->ap_Flags &= ~APF_DIDDIR;
match = IDOS->MatchNext(m_anchor);
}
if ( match != 0 ) {
IDOS->SetIoErr(match);
IDOS->MatchEnd(m_anchor);
throw dos_error();
}
else {
m_searching = true;
m_lock = IDOS->CurrentDir(m_anchor->ap_Current->an_Lock);
return m_anchor->ap_Info.fib_FileName;
}
}
const char* PatternMatch::nextFile()
{
if ( !m_searching ) {
IDOS->SetIoErr(ERROR_OBJECT_NOT_FOUND);
throw dos_error();
}
IDOS->CurrentDir(m_lock); // restore lock
int32 match = IDOS->MatchNext(m_anchor);
while ( match == 0 && m_anchor->ap_Info.fib_DirEntryType > 0 ) {
m_anchor->ap_Flags &= ~APF_DIDDIR;
match = IDOS->MatchNext(m_anchor);
}
if ( match == 0 ) {
m_lock = IDOS->CurrentDir(m_anchor->ap_Current->an_Lock);
return m_anchor->ap_Info.fib_FileName;
}
else if ( match == ERROR_NO_MORE_ENTRIES ) {
IDOS->MatchEnd(m_anchor);
m_searching = false;
return 0;
}
else {
IDOS->SetIoErr(match);
IDOS->MatchEnd(m_anchor);
m_searching = false;
throw dos_error();
}
}